/******************************************************************************
Paper: The Impact of Welfare on Intergroup Relations
Author: Akshay Dixit

CPHS: This .do file merges CPHS datasets (member income, aspirational india,
income pyramids, consumption pyramids)
******************************************************************************/

clear all
gl data_cmie "$identity/data/cphs"

cd "$data_cmie"

****************************************************************************** 

*** Member income data ***

	/*
		This is an individual-level dataset, recording data on individual
		household members.
		
		The other datasets below are household-level datasets.
	*/

local dates 20170531 20170630 20170731 20170831 20170930 20171031 20171130 20171231 20180131 20180228 20180331 20180430 20180531 20180630 20180731 20180831 20180930 20181031 20181130 20181231 20190131 20190228 20190331 20190430

foreach d of local dates {
	
u "member_income_`d'_MS_rev.dta", clear


	// Retain people successfully surveyed during the particular wave of data collection
keep if response_status == "Accepted"

	// Create variables denoting if the individual is farmer or laborer
g farmer = (nature_of_occupation == "Small Farmer" | nature_of_occupation == "Organised Farmer")
g laborer = (nature_of_occupation == "Agricultural Labourer")

bysort hh_id: egen prop_farmer = mean(farmer)
bysort hh_id: egen prop_laborer = mean(laborer)
bysort hh_id: egen num_farmer = total(farmer)
bysort hh_id: egen num_laborer = total(laborer)

	// Retain data on household head
keep if relation_with_hoh == "HOH"

	// Caste and caste category
g caste_category_nonmissing = caste_category
replace caste_category_nonmissing = "" if caste_category == "Not Applicable" | caste_category == "Not Stated"

g caste_nonmissing = caste
replace caste_nonmissing = "" if caste == "Not Applicable" | caste == "Caste not stated"

	// Education
g edu = .
replace edu = 0 if education == "No Education"
replace edu = 1 if education == "1st Std. Pass"
replace edu = 2 if education == "2nd Std. Pass"
replace edu = 3 if education == "3rd Std. Pass"
replace edu = 4 if education == "4th Std. Pass"
replace edu = 5 if education == "5th Std. Pass"
replace edu = 6 if education == "6th Std. Pass"
replace edu = 7 if education == "7th Std. Pass"
replace edu = 8 if education == "8th Std. Pass"
replace edu = 9 if education == "9th Std. Pass"
replace edu = 10 if education == "10th Std. Pass"
replace edu = 11 if education == "11th Std. Pass"
replace edu = 12 if education == "12th Std. Pass"
replace edu = 13 if education == "Graduate"
replace edu = 14 if education == "Post Graduate"
replace edu = 15 if education == "Ph.D / M.Phil"

	// Age
g age = age_yrs + (age_mths/12)
replace age = . if age < 0

	// Gender
g female = (gender == "F")
replace female = . if gender == "Data Not Available"

	// Keep relevant variables, save
keep hh_id district member_status state month* *caste* religion edu age female prop_farmer prop_laborer num_laborer num_farmer

replace month = subinstr(month," 20","",.)
replace month = lower(month)

levelsof month, clean local(mn)
ren * *_`mn'
ren hh_id_`mn' hh_id

save, replace

}

******************************************************************************

*** Aspirational India data *** 

local dates 20170501_20170831 20170901_20171231 20180101_20180430 20180501_20180831 20180901_20181231 20190101_20190430
foreach d of local dates {
	
u "aspirational_india_`d'_R.dta", clear

	// Retain people successfully surveyed during the particular wave of data collection
keep if response_status == "Accepted"	

qui summ wave_no
local wave = r(mean)

keep hh_id *_owned *borrow* v*

	// Convert string yes/no variables to numeric
unab borrow: *borrow*
foreach var of local borrow {
	replace `var' = "1" if `var' == "Y"
	replace `var' = "0" if `var' == "N"
	replace `var' = "" if `var' == "NA"
	destring `var', replace
}

	// Convert string yes/no variables to numeric
unab v: v*
foreach var of local v {
	replace `var' = "1" if `var' == "Y"
	replace `var' = "0" if `var' == "N"
	replace `var' = "" if `var' == "NA"	
	destring `var', replace
}

	// Rename variables to make space to add wave number suffix
rename *_for_* *_*
rename *_from_* *_*	
	
ren * *_`wave'
ren hh_id_`wave' hh_id

save, replace
	
}

******************************************************************************

*** Household Income data ***

local dates 20170531 20170630 20170731 20170831 20170930 20171031 20171130 20171231 20180131 20180228 20180331 20180430 20180531 20180630 20180731 20180831 20180930 20181031 20181130 20181231 20190131 20190228 20190331 20190430
foreach d of local dates {

u "household_income_`d'_MS_rev.dta", clear

	// Retain people successfully surveyed during the particular wave of data collection
keep if response_status == "Accepted"	

cap drop reason_for_non_response hh_weight_ms-hh_non_response_for_state_ms

	// Rename variables to make space to add wave number suffix
rename *_from_* *_*	
rename *_of_* *_*

replace month = subinstr(month," 20","",.)
replace month = lower(month)

levelsof month, clean local(mn)
ren * *_`mn'
ren hh_id_`mn' hh_id

save, replace
	
}

******************************************************************************

*** Consumption pyramids ***

local dates 20170531 20170630 20170731 20170831 20170930 20171031 20171130 20171231 20180131 20180228 20180331 20180430 20180531 20180630 20180731 20180831 20180930 20181031 20181130 20181231 20190131 20190228 20190331 20190430
foreach d of local dates {

u "consumption_pyramids_`d'_MS_rev.dta", clear

	// Retain people successfully surveyed during the particular wave of data collection
keep if response_status == "Accepted"	

cap drop reason_for_non_response hh_weight_ms-hh_non_response_for_state_ms

keep hh_id state district region_type stratum psu_id month_slot month response_status total_expenditure adj_total_expenditure monthly_expense_on_meat_eggs_and adj_monthly_expense_on_meat_eggs monthly_expense_on_meat_and_fish adj_monthly_expense_on_meat_and_ monthly_expense_on_eggs adj_monthly_expense_on_eggs monthly_expense_on_intoxicants adj_monthly_expense_on_intoxican monthly_expense_on_liquor monthly_expense_on_liquor_in_res monthly_expense_on_health_insura monthly_expense_on_social_obliga monthly_expense_on_religious_obl monthly_expense_on_health

	// Rename variables to make space to add wave number suffix
rename monthly_* *
rename *_monthly_* *_*

replace month = subinstr(month," 20","",.)
replace month = lower(month)

levelsof month, clean local(mn)
ren * *_`mn'
ren hh_id_`mn' hh_id

save, replace
	
}

******************************************************************************

*** Merge all datasets using hh_id ***

	// Income pyramids
u "household_income_20170531_MS_rev.dta", clear

local dates 20170630 20170731 20170831 20170930 20171031 20171130 20171231 20180131 20180228 20180331 20180430 20180531 20180630 20180731 20180831 20180930 20181031 20181130 20181231 20190131 20190228 20190331 20190430

foreach d of local dates {
	merge 1:1 hh_id using "household_income_`d'_MS_rev.dta", nogen
}

local dates 20170531 20170630 20170731 20170831 20170930 20171031 20171130 20171231 20180131 20180228 20180331 20180430 20180531 20180630 20180731 20180831 20180930 20181031 20181130 20181231 20190131 20190228 20190331 20190430

foreach d of local dates {
	merge 1:1 hh_id using "member_income_`d'_MS_rev.dta", nogen
}

save "all_household_income.dta", replace

	// Consumption pyramids
u "all_household_income.dta", clear

local dates 20170531 20170630 20170731 20170831 20170930 20171031 20171130 20171231 20180131 20180228 20180331 20180430 20180531 20180630 20180731 20180831 20180930 20181031 20181130 20181231 20190131 20190228 20190331 20190430

foreach d of local dates {
	merge 1:1 hh_id using "consumption_pyramids_`d'_MS_rev.dta", nogen
}

save "all_household_income_consumption.dta", replace
erase "all_household_income.dta"


	// Aspirational India
u "all_household_income_consumption.dta", clear

local dates 20170501_20170831 20170901_20171231 20180101_20180430 20180501_20180831 20180901_20181231 20190101_20190430
foreach d of local dates {
	merge 1:1 hh_id using "aspirational_india_`d'_R.dta", nogen
}

save "merged_data.dta", replace
erase "all_household_income_consumption.dta"

******************************************************************************

clear

